package com.graduate.twentyone.mapper;

import com.graduate.twentyone.domain.Food;
import com.graduate.twentyone.domain.result.QuerySaleByCate;
import com.graduate.twentyone.domain.result.RecentMonth;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FoodMapper {
    @Insert("insert into food  values(null,#{name},#{picture},#{price},#{mold},#{des},1,1)")
    Integer addfood(Food food);

    @Update("<script>"
            + "update food"
            + "<set>"
            + " <if test='name != null'> name=#{name}, </if> "
            + " <if test='picture != null'> picture=#{picture}, </if> "
            + " <if test='price != null'> price=#{price}, </if> "
            + " <if test='mold != null'> mold=#{mold}, </if> "
            + " <if test='des != null'> des=#{des}, </if> "
            + " <if test='status != null'> status=#{status}, </if> "
            + " <if test='undercarriage != null'> undercarriage=#{undercarriage}, </if> "
            + "</set> "
            + "where fid = #{fid} "
            + "</script>")
    Integer updatefood(Food food);

    @Select("select * from food where status = 1 order by  fid  desc")
    List<Food> selectfood();

    @Select("select * from food where status = 1 and undercarriage=1 order by  fid  desc")
    List<Food> clientselectfood();

    @Select("SELECT sale.fprice, sale.fname, sum(num) as count FROM sale WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(datetime) GROUP BY sale.fname")
    List<RecentMonth> queryRecentMonth();

    @Select("SELECT cate.cate_name,count(*)as amount,sum(sale.fprice) as all_price FROM cate,food,sale WHERE cate.cate_id = food.mold AND food.fid = sale.fid GROUP BY cate.cate_name")
    List<QuerySaleByCate> querySaleByCate();
}
